package com.cn.dtalk.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * 发送钉钉消息体，继承自{@link HashMap}
 * 添加方法{@link #add(Object, Object)}，链式编程
 *
 * @author: WGQ
 * @data: 2020/10/23 9:21
 * @desc:
 */
public class Message<K, V> extends HashMap {


    // new 一个实例
    public static Message getInstance() {
        return new Message<String, Object>();
    }

    /**
     * 链式编程
     *
     * @param k k
     * @param v v
     * @return this
     */
    public Message<K, V> add(K k, V v) {
        this.put(k, v);
        return this;
    }

    public Message<K, V> of(K k, V v) {
        return getInstance().add(k, v);
    }


    /**
     * at 信息
     *
     * @param mobileList at手机号列表
     * @param isAtAll    是否at 所有人
     * @return
     */
    public Message at(List<String> mobileList, boolean isAtAll) {
        List<String> mobiles = new ArrayList<>();
        if (!isAtAll) {
            if (this.get("at") == null) {
                mobiles = mobileList;
            } else {
                if (isAtAll) {
                    return null;
                }
                Message at = (Message) this.get("at");
                List<String> atMobiles = (List<String>) at.get("atMobiles");
                atMobiles.addAll(mobileList);   // 将二者手机号合并
                mobiles = atMobiles;
            }
        }
        Message atMobiles = Message.getInstance().add("atMobiles", mobiles);
        atMobiles.add("isAtAll", isAtAll);
        this.put("at", atMobiles);
        return this;
    }

    /**
     * 纯文本 消息格式
     * {
     * "msgtype": "text",
     * "text": {
     * "content": "我就是我, 是不一样的烟火@156xxxx8827"
     * },
     * "at": {
     * "atMobiles": [
     * "156xxxx8827",
     * "189xxxx8325"
     * ],
     * "isAtAll": false
     * }
     * }
     * <p>
     * 这里拼接消息使用装饰器模式
     */
    public static class TextMessage extends Message {

        public static TextMessage getInstance() {
            TextMessage instance = new TextMessage();
            instance.add("msgtype", MessageType.text);
            return instance;
        }

        // 消息体  不在链式编程不能返回任何值，因为消息提只能有一个
        public void setMessage(String message) {
            Message messageBody = Message.getInstance().add("content", message);
            this.put(MessageType.text, messageBody);
        }


    }

    /**
     * link 消息
     */
    public static class LinkMessage extends Message {

        // 创建link 消息实例
        public static LinkMessage getInstance() {
            LinkMessage instance = new LinkMessage();
            instance.add("msgtype", MessageType.link);
            return instance;
        }

        public LinkMessage setBody(LinkMessageBody body) {
            this.add("link", body);
            return this;
        }

        public static class LinkMessageBody extends Message {
            private String text;        // 必填
            private String title;       // 必填
            private String picUrl;      // 非必填
            private String messageUrl;  // 必填

            public static LinkMessageBody getInstance() {
                return new LinkMessageBody();
            }

            public LinkMessageBody setText(String text) {
                this.add("text", text);
                return this;
            }

            public LinkMessageBody setTitle(String title) {
                this.add("title", title);
                return this;
            }

            public LinkMessageBody setPicUrl(String picUrl) {
                this.add("picUrl", picUrl);
                return this;
            }

            public LinkMessageBody setMessageUrl(String messageUrl) {
                this.add("messageUrl", messageUrl);
                return this;
            }
        }
    }

    /**
     * markdown 消息
     */
    public static class MarkDownMessage extends Message {
        // 创建markdown 消息实例
        public static MarkDownMessage getInstance() {
            MarkDownMessage instance = new MarkDownMessage();
            instance.add("msgtype", MessageType.markdown);
            return instance;
        }

        public MarkDownMessage setBody(MarkDownMessage.MarkDownMessageBody body) {
            this.add("markdown", body);
            return this;
        }

        public static class MarkDownMessageBody extends MarkDownMessage{
            private String title;
            private String text;

            public static MarkDownMessageBody getInstance(){
                return new MarkDownMessageBody();
            }

            public MarkDownMessageBody setTitle(String title){
                this.add("title",title);
                return this;
            }
            public MarkDownMessageBody setText(String text){
                this.add("text",text);
                return this;
            }
        }

    }


    /**
     * 整体跳转ActionCard类型
     */
    public static class WholeJumpActionCardMessage extends Message {
        // 创建markdown 消息实例
        public static WholeJumpActionCardMessage getInstance() {
            WholeJumpActionCardMessage instance = new WholeJumpActionCardMessage();
            instance.add("msgtype", MessageType.actionCard);
            return instance;
        }
        // 设置消息体
        public WholeJumpActionCardMessage setBody(WholeJumpActionCardMessage.WholeJumpActionCardMessageBody body) {
            this.add("actionCard", body);
            return this;
        }

        public static class WholeJumpActionCardMessageBody extends WholeJumpActionCardMessage{
            private String title;
            private String text;
            // 非必填，其余的都必填
            private String btnOrientation;  // 0-按钮竖直排列，1-按钮横向排列
            private String singleTitle;     // 单个按钮的标题。(设置此项和singleURL后btns无效)
            private String singleURL;       // 点击singleTitle按钮触发的URL

            public static WholeJumpActionCardMessageBody getInstance(){
                return new WholeJumpActionCardMessageBody();
            }

            public WholeJumpActionCardMessageBody setTitle(String title){
                this.add("title",title);
                return this;
            }
            public WholeJumpActionCardMessageBody setText(String text){
                this.add("text",text);
                return this;
            }

            public WholeJumpActionCardMessageBody setBtnOrientation(String btnOrientation){
                this.add("btnOrientation",btnOrientation);
                return this;
            }

            public WholeJumpActionCardMessageBody setSingleTitle(String singleTitle){
                this.add("singleTitle",singleTitle);
                return this;
            }

            public WholeJumpActionCardMessageBody setSingleURL(String singleURL){
                this.add("singleURL",singleURL);
                return this;
            }

        }

    }

    /**
     * independent
     *独立跳转ActionCard类型
     */
    public static class IndependentJumpActionCardMessage extends Message {
        // IndependentJumpActionCardMessage 消息实例
        public static IndependentJumpActionCardMessage getInstance() {
            IndependentJumpActionCardMessage instance = new IndependentJumpActionCardMessage();
            instance.add("msgtype", MessageType.actionCard);
            return instance;
        }
        // 设置消息体
        public IndependentJumpActionCardMessage setBody(IndependentJumpActionCardMessage.IndependentJumpActionCardMessage body) {
            this.add("actionCard", body);
            return this;
        }

        public static class IndependentJumpActionCardMessageBody extends IndependentJumpActionCardMessage{
            private String title;
            private String text;
            // 非必填，其余的都必填
            private String btnOrientation;  // 0-按钮竖直排列，1-按钮横向排列

            private List<Btn> btns;     //按钮

            public static IndependentJumpActionCardMessageBody getInstance(){
                return new IndependentJumpActionCardMessageBody();
            }

            public IndependentJumpActionCardMessageBody setTitle(String title){
                this.add("title",title);
                return this;
            }
            public IndependentJumpActionCardMessageBody setText(String text){
                this.add("text",text);
                return this;
            }

            public IndependentJumpActionCardMessageBody setBtnOrientation(String btnOrientation){
                this.add("btnOrientation",btnOrientation);
                return this;
            }

            public IndependentJumpActionCardMessageBody setBtns(List<Btn> btns){
                this.add("btns",btns);
                return this;
            }

            // 添加单个
            public IndependentJumpActionCardMessageBody addBtn(Btn btn){
                Object btns = this.get("btns");
                List<Btn> _btns = new ArrayList<>();
                if (btns==null){
                    _btns.add(btn);
                }else {
                    _btns.addAll((Collection<? extends Btn>) btns);
                    _btns.add(btn);
                }
                this.add("btns",_btns);
                return this;
            }

            public static class Btn extends Message{
                // title,actionURL
                public static Btn getInstance(){
                    return new Btn();
                }

                public Btn setTitle(String title){
                    this.add("title",title);
                    return this;
                }

                public Btn setActionUrl(String actionUrl){
                    this.add("actionURL",actionUrl);
                    return this;
                }
            }
        }
    }

    /**
     * FeedCard类型
     */
    public static class FeedCardMessage extends  Message{

        // FeedCardMessage 消息实例
        public static FeedCardMessage getInstance() {
            FeedCardMessage instance = new FeedCardMessage();
            instance.add("msgtype", MessageType.feedCard);
            return instance;
        }
        // 设置消息体
        public FeedCardMessage setBody(FeedCardMessage.FeedCardMessageBody body) {
            this.add("feedCard", body);
            return this;
        }

        public static class FeedCardMessageBody extends FeedCardMessage{

            private List<FeedCardMessageBody.Link> links;     //按钮

            public static FeedCardMessageBody getInstance(){
                return new FeedCardMessageBody();
            }

            public FeedCardMessageBody setLinks(List<FeedCardMessageBody.Link> links){
                this.add("links",links);
                return this;
            }

            // 添加单个
            public FeedCardMessageBody addBtn(FeedCardMessageBody.Link link){
                Object links = this.get("links");
                ArrayList<FeedCardMessageBody.Link> _links = new ArrayList<>();
                if (links==null){
                    _links.add(link);
                }else {
                    _links = (ArrayList<Link>) links;
                    _links.add(link);
                }
                this.add("btns",_links);
                return this;
            }

            public static class Link extends Message{
                // title,actionURL
                public static FeedCardMessageBody.Link getInstance(){
                    return new FeedCardMessageBody.Link();
                }

                public FeedCardMessageBody.Link setTitle(String title){
                    this.add("title",title);
                    return this;
                }

                public FeedCardMessageBody.Link setMessageURL(String messageURL){
                    this.add("messageURL",messageURL);
                    return this;
                }
                public FeedCardMessageBody.Link setPicURL(String picURL){
                    this.add("picURL",picURL);
                    return this;
                }
            }
        }
    }
}
